package com.wx_shop.api.controller.api;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wx_shop.api.comm.DValid;
import com.wx_shop.api.service.ShopCartService;
import com.wx_shop.api.vo.BaseRes;
import com.wx_shop.api.vo.ShopcartVo;

/**
 * 购物车
 * list update_item add_item del_item  
 * 
 */
@Controller
@RequestMapping("api/shopcart")
public class ShopCartRest {
	
	@Resource
	private ShopCartService shopCartService;
	
	@ResponseBody
	@RequestMapping("cartList")
	public BaseRes<List<ShopcartVo>> cartList() {
		int userId = 1;
		BaseRes<List<ShopcartVo>> res = shopCartService.cartList(userId);
		return res;
	}
	
	/**
	 * 更新购物车数据
	 * @param itemId
	 * @param type
	 * @param val
	 * @return
	 */
	@ResponseBody
	@RequestMapping("updateCartItem")
	public BaseRes<String> updateCartItem(
			@Validated(value = { DValid.ModifyEntity.class }) 
			@RequestBody 
			ShopcartVo shopcartVo, 
			BindingResult bindingResult
			) {
		
		Integer itemId = shopcartVo.getId();
		Integer val = shopcartVo.getNum();
		
		int userId = 1;
		BaseRes<String> res = shopCartService.updateCartItem(userId, itemId, val);
		return res;
	}
	
	/**
	 * 删除购物车-项
	 * @param itemId
	 * @return
	 */
	@ResponseBody
	@RequestMapping("delCartItem")
	public BaseRes<String> delCartItem(Integer itemId) {
		int userId = 1;
		BaseRes<String> res = shopCartService.delCartItem(userId, itemId);
		return res;
	}
	
	@ResponseBody
	@RequestMapping( value = "addCartItem", method = RequestMethod.POST)
	public BaseRes<String> addCartItem(
			@Validated(value = { DValid.AddEntity.class }) 
			@RequestBody 
			ShopcartVo shopcartVo, 
			BindingResult bindingResult) {
		
		int userId = 1;
		return shopCartService.addCartItem(userId, shopcartVo);
	}
	
}
